package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.TransMonitorInfoBean;
import cc.rengu.igas.bomp.facade.request.TransMonitorRequest;
import cc.rengu.igas.bomp.facade.response.TransMonitorResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.MonitorMode;
import cc.rengu.oltp.service.model.TransMonitorInfo;
import cc.rengu.oltp.service.realize.TransMonitorService;
import cc.rengu.oltp.service.realize.impl.TransMonitorServiceImpl;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: zhangxuran
 * @Date: 2020/5/11 14:56
 * @Description: 交易监控
 */
public class BompTransMonitorService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        TransMonitorRequest request = new TransMonitorRequest();
        ConvertUtil.convertOutput(request);
        return request;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        TransMonitorRequest transMonitorRequest = (TransMonitorRequest) request;
        BizResponse<TransMonitorResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        TransMonitorResponse transMonitorResponse = new TransMonitorResponse();


        TransMonitorService transMonitorService = new TransMonitorServiceImpl();
        MonitorMode monitorMode = new MonitorMode();
        BeanUtil.beanCopy(transMonitorRequest, monitorMode);
        monitorMode.setTxnNum(transMonitorRequest.getInnerTxnNum());
        monitorMode.setInstId(transMonitorRequest.getHeader().getInstId());
        List<TransMonitorInfo> transMonitorInfoList = transMonitorService.subscribeTransMonitorInfo(monitorMode);

        if (!CollectionUtils.isEmpty(transMonitorInfoList)) {
            List<TransMonitorInfoBean> transMonitorInfoBeanList = transMonitorInfoList.stream().map(this::convertToTransMonitorInfoBean).collect(Collectors.toList());
            transMonitorResponse.setTransMonitorInfoBeanList(transMonitorInfoBeanList);
            transMonitorResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            transMonitorResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        } else {
            transMonitorResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            transMonitorResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        }

        bizResponse.setResult(transMonitorResponse);
        bizResponse.setRespCode(transMonitorResponse.getRespCode());
        bizResponse.setRespDesc(transMonitorResponse.getRespDesc());
        return bizResponse;
    }

    private TransMonitorInfoBean convertToTransMonitorInfoBean(TransMonitorInfo transMonitorInfo) {
        TransMonitorInfoBean transMonitorInfoBean = new TransMonitorInfoBean();
        BeanUtil.beanCopy(transMonitorInfo, transMonitorInfoBean);
        transMonitorInfoBean.setInnerTxnNum(transMonitorInfo.getTxnNum());
        transMonitorInfoBean.setTxnStatus(transMonitorInfo.getTransStatus());
        return transMonitorInfoBean;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        TransMonitorResponse transMonitorResponse = (TransMonitorResponse) bizResponse.getResult();
        ConvertUtil.convertInput(transMonitorResponse);
    }
}
